from django.shortcuts import render
from .models import Movie,Category
from django.http import JsonResponse,Http404
from .serializers import MovieSerializer,CategorySerializer
from rest_framework.decorators import api_view
from rest_framework import status
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework import generics
from rest_framework import mixins
from rest_framework import viewsets
from utils.filters import MovieFilter
from django_filters import rest_framework as filters
# from rest_framework.permissions import IsAdminUser
from .permissions import IsAdminUserOrReadOnly

# Create your views here.

# @api_view(['GET','POST'])
# def movie_list(request):
    
#     if request.method == 'GET':
#         movies = Movie.objects.all()
#         serializer = MovieListSerializer(movies,many=True)  # 传入对象要设置many=true
        
#         return Response(serializer.data, status=status.HTTP_200_OK)     # 很多时候不显示要设置safe=False
    
    
#     elif request.method == "POST":
        
#         serializer = MovieListSerializer(data = request.data)
        
#         if serializer.is_valid():
#             serializer.save()
#             return Response(serializer.data,status=status.HTTP_201_CREATED)
        
#         return Response(serializer.errors,status=status.HTTP_400_BAD_REQUEST)
        

# 基于drf的类视图
# class MovieDetail(APIView):
    
    
#     def get(self,request,pk):      # request这是是drf的
#         try:
#             movie = Movie.objects.get(pk = pk)
#         except:
#             raise Http404
#         serializer = MovieDetailSerializer(movie)
#         return Response(serializer.data)                # 返回数据
    
    
#     def put(self,request,pk):
#         try:
#             movie = Movie.objects.get(pk = pk)
#         except:
#             raise Http404
#         serializer = MovieDetailSerializer(movie, data=request.data,partial=True)
#         if serializer.is_valid():
#             serializer.save()
#             return Response(serializer.data,status=status.HTTP_202_ACCEPTED)                # 返回数据
        
#         return Response(serializer.errors,status=status.HTTP_400_BAD_REQUEST)
    
    
#     def delete(self,request,pk):
#         try:
#             movie = Movie.objects.get(pk = pk)
#         except:
#             raise Http404
#         movie.delete()
#         return Response(status=status.HTTP_204_NO_CONTENT)




# 通用视图
# class movie_list(generics.ListCreateAPIView):
#     queryset = Movie.objects.all()
#     serializer_class = MovieListSerializer







# # 通用视图,更方便的增删改查
# class MovieDetail(generics.RetrieveUpdateDestroyAPIView):
#     queryset = Movie.objects.all()
#     serializer_class = MovieDetailSerializer
    
    
# class MovieFilter(filters.FilterSet):
#     # 模糊查询
#     movie_name = filters.CharFilter(lookup_expr="icontains")
#     category_id = filters.NumberFilter()
#     region = filters.NumberFilter()

#     class Meta:
#         model =  Movie
#         fields = ['movie_name','category_id','region']

# 视图集，提供简单的增删改查操作
class MovieViewSet(viewsets.ModelViewSet):
    queryset = Movie.objects.all()
    serializer_class = MovieSerializer
    
    filter_backends = (filters.DjangoFilterBackend,)
    # filterset_fields = ('movie_name',)     # 过滤的名字
    
    # 自定义查询的类型：实现模糊查询
    filterset_class = MovieFilter
    permission_classes = [IsAdminUserOrReadOnly]


    
    
    
    # 可以继续重写方法实现自定义功能
    
    
class CategoryViewSet(viewsets.ModelViewSet):
    queryset = Category.objects.all()
    serializer_class = CategorySerializer
    permission_classes = [IsAdminUserOrReadOnly]
    



